package com.example.elm.Login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elm.Email.SendEmail;
import com.example.elm.R;

import java.util.Random;

public class Login_Page extends AppCompatActivity {

    LinearLayout login_button;
    TextView login_button_text;
    EditText set_email;
    CheckBox is_read;
    ImageView close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);

        login_button = findViewById(R.id.login_button);
        set_email = findViewById(R.id.set_email);
        login_button_text = findViewById(R.id.login_button_text);
        is_read = findViewById(R.id.is_read);
        close = findViewById(R.id.close);

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        set_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!set_email.getText().toString().isEmpty()) {
                    login_button.setBackgroundColor(Color.YELLOW);
                    login_button_text.setTextColor(Color.BLACK);
                }
                else {
                    login_button.setBackgroundColor(Color.rgb(252, 243, 202));
                    login_button_text.setTextColor(Color.rgb(128, 128, 128));
                }
            }
        });

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!set_email.getText().toString().isEmpty() && is_read.isChecked()) {
                    String sms = sms_code();
                    new SendEmail(set_email.getText().toString(), sms, "饿了没", "【饿了没】"+ sms + "（登录验证码，请完成验证），如非本人操作，请忽略本短信。").sendemail();

                    Intent intent = new Intent(Login_Page.this, Input_SMS_Code_Page.class);
                    intent.putExtra("email", set_email.getText().toString());
                    intent.putExtra("sms", sms);
                    startActivity(intent);
                }
                else if (!is_read.isChecked()) {
                    Toast.makeText(Login_Page.this, "请先阅读并同意协议", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public String sms_code() {
        String ans = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            ans += String.valueOf(random.nextInt(9));
        }
        System.out.println(ans);
        return ans;
    }
}